package com.yanqu.road.dao.impl.activity.greattrade;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.greattrade.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserGreatTradeDataDaoImpl extends TempDataObjectDao<UserGreatTradeData> {

    public static String getUserGreatTradeDataSql = "select * from `t_u_great_trade_user` where `activityId`=? and `userId`=?";
    public UserGreatTradeData getUserGreatTradeData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserGreatTradeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserGreatTradeDataSql = "insert into `t_u_great_trade_user`(activityId,userId,dayFreeTimes,dayTradeTimes,totalTradeTimes,buyGoldToken,buySliverToken,completeTimes,maxTitle,maxAssets,eventMap,beautyMap,tradeStatus,goodsList,goodsPriceList,goodsHistoryPrice,industryPrice,industry,areaEventMap,saltWorksMap,milepostRecordList,profitMap,dayResetTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    
    @Override
    protected boolean addData(UserGreatTradeData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDayFreeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDayTradeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalTradeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyGoldToken()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuySliverToken()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCompleteTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMaxTitle()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMaxAssets()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBeautyMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getTradeStatus())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGoodsList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGoodsPriceList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGoodsHistoryPrice())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getIndustryPrice())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getIndustry())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAreaEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getSaltWorksMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMilepostRecordList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getProfitMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDayResetTime()));
        return getDb().executeNoneQuery(insertUserGreatTradeDataSql, para) > 0;
    }

    public static String updateUserGreatTradeDataSql = "update `t_u_great_trade_user` set dayFreeTimes=?,dayTradeTimes=?,totalTradeTimes=?,buyGoldToken=?,buySliverToken=?,completeTimes=?,maxTitle=?,maxAssets=?,eventMap=?,beautyMap=?,tradeStatus=?,goodsList=?,goodsPriceList=?,goodsHistoryPrice=?,industryPrice=?,industry=?,areaEventMap=?,saltWorksMap=?,milepostRecordList=?,profitMap=?,dayResetTime=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(UserGreatTradeData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getDayFreeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDayTradeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalTradeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyGoldToken()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuySliverToken()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCompleteTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMaxTitle()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMaxAssets()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBeautyMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getTradeStatus())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGoodsList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGoodsPriceList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGoodsHistoryPrice())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getIndustryPrice())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getIndustry())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAreaEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getSaltWorksMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMilepostRecordList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getProfitMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDayResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        return getDb().executeNoneQuery(updateUserGreatTradeDataSql, para) > 0;
    }

    @Override
    protected UserGreatTradeData parseData(ResultSet rs) throws SQLException {
        UserGreatTradeData data = new UserGreatTradeData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setDayFreeTimes(rs.getInt("dayFreeTimes"));
        data.setDayTradeTimes(rs.getInt("dayTradeTimes"));
        data.setTotalTradeTimes(rs.getInt("totalTradeTimes"));
        data.setBuyGoldToken(rs.getInt("buyGoldToken"));
        data.setBuySliverToken(rs.getInt("buySliverToken"));
        data.setCompleteTimes(rs.getInt("completeTimes"));
        data.setMaxTitle(rs.getInt("maxTitle"));
        String str = rs.getString("maxAssets");
        if(!StringUtils.isNullOrEmpty(str)) {
            data.setMaxAssets(str);
        }
        data.setEventMap(JSON.parseObject(rs.getString("eventMap"), new TypeReference<Map<Integer, Integer>>() {}));
        data.setBeautyMap(JSON.parseObject(rs.getString("beautyMap"), new TypeReference<Map<Integer, GreatTradeBeautySkill>>() {}));
        data.setTradeStatus(JSON.parseObject(rs.getString("tradeStatus"), GreatTradeSimpleStatus.class));
        data.setGoodsList(JSON.parseObject(rs.getString("goodsList"), new TypeReference<List<GreatTradeGoods>>() {}));
        data.setGoodsPriceList(JSON.parseObject(rs.getString("goodsPriceList"), new TypeReference<Map<Integer, Integer>>() {}));
        data.setGoodsHistoryPrice(JSON.parseObject(rs.getString("goodsHistoryPrice"), new TypeReference<Map<Integer, List<Integer>>>(){}));
        data.setIndustryPrice(JSON.parseObject(rs.getString("industryPrice"), new TypeReference<List<Map<Integer, Long>>>() {}));
        data.setIndustry(JSON.parseObject(rs.getString("industry"), GreatTradeIndustry.class));
        data.setAreaEventMap(JSON.parseObject(rs.getString("areaEventMap"), new TypeReference<Map<Integer, GreatTradeAreaEvent>>() {}));
        data.setSaltWorksMap(JSON.parseObject(rs.getString("saltWorksMap"), new TypeReference<Map<Integer, GreatTradeSaltWorks>>() {}));
        data.setMilepostRecordList(JSON.parseObject(rs.getString("milepostRecordList"), new TypeReference<List<GreatTradeMilepostRecord>>() {}));
        data.setProfitMap(JSON.parseObject(rs.getString("profitMap"), new TypeReference<List<Map<Integer, String>>>() {}));
        data.setDayResetTime(rs.getInt("dayResetTime"));

        return data;
    }
}
